/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.dao.impl;

import cz.muni.fi.pa165.bookingmanager.dao.AdministratorManager;
import cz.muni.fi.pa165.bookingmanager.entities.Administrator;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.SQLIntegrityConstraintViolationException;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;

/**
 *
 * @author Radek Koubský
 */
@Stateless
public class AdministratorManagerImpl implements Serializable, AdministratorManager {
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName = "BookingManagerPU")
    private EntityManager em;

    @Override
    public void createAdministrator(Administrator administrator) throws IllegalOrphanException, PreexistingEntityException {
       if (administrator == null) {
            throw new IllegalArgumentException();
        }

        List<String> illegalOrphanMessages = null;
        if (administrator.getId() != null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("Administrator id is not null, Administrator cant be added to database ");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        if (administrator.getFirstName().equals("")) {
            throw new EJBException("name should not be null");
        }        
        if (administrator.getSurName().equals("")) {
            throw new EJBException("surname should not be null");
        }
        try{
        em.persist(administrator);
        em.flush();
        }
        catch(PersistenceException ex)
        {
            
            if(ex.getCause().getCause().getClass().equals(SQLIntegrityConstraintViolationException.class))
                throw  new PreexistingEntityException(null);
        }
       
    }

    @Override
    public void updateAdministrator(Administrator administrator) throws IllegalOrphanException, PreexistingEntityException, NonexistentEntityException, Exception {
        if (administrator == null) {
            throw new IllegalArgumentException();
        }

        try {
            List<String> illegalOrphanMessages = null;
            if (administrator.getId() == null) {
                illegalOrphanMessages = new ArrayList<String>();
                illegalOrphanMessages.add("Administrator id is null");
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (administrator.getFirstName().equals("")) {
                throw new EJBException("name should not be null");
            }
            if (administrator.getSurName().equals("")) {
                throw new EJBException("surname should not be null");
            }
            em.merge(administrator);

        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = administrator.getId();
                if (getAdministratorById(id) == null) {
                    throw new NonexistentEntityException("The administrator with id " + id + " no longer exists.");
                }
            }
             if(ex.getCause().getCause().getClass().equals(SQLIntegrityConstraintViolationException.class))
                throw  new PreexistingEntityException(null);
             else
             {
                throw ex;
             }
            
            
        }
    }

    @Override
    public void deleteAdministrator(Administrator administrator) throws NonexistentEntityException, IllegalOrphanException {
        if (administrator == null) {
            throw new IllegalArgumentException("Administrator is null");
        }

        List<String> illegalOrphanMessages = null;
        if (administrator.getId() == null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("administrator id is null");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }

        try {
            em.remove(em.merge(administrator));
            administrator.setId(null);
        } catch (IllegalArgumentException ex) {
            throw new NonexistentEntityException("The administrator no longer exists.");
        }
    }

    @Override
    public Administrator getAdministratorById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }
        return em.find(Administrator.class, id);
    }
    
    @Override
    public Administrator getAdministratorByUserName(String username) {
        if (username == null) {
            throw new IllegalArgumentException();
        }
        Administrator admin;
        Query q = em.createQuery("select object(o) from Administrator as o where o.username='" + username + "'");
        try{
        admin=(Administrator) q.getSingleResult();  
        return admin;
        }
        catch(NoResultException ex)
        {
            return null;
        }
        
        
    }

    @Override
    public List<Administrator> findAllAdministrators() {
        Query q = em.createQuery("select object(o) from Administrator as o");
        return q.getResultList();
    }
}
